package org.edu.modules.course.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.config.oss.OSSUtils;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.system.query.QueryGenerator;
import org.edu.common.system.vo.LoginUser;
import org.edu.common.util.TimeStampUtil;
import org.edu.modules.course.entity.VeCuzCourse;
import org.edu.modules.course.mapper.VeCuzCourseMapper;
import org.edu.modules.course.service.IVeCuzCourseService;
import org.edu.modules.course.vo.VeCuzCourseVo;
import org.edu.modules.liancategory.entity.VeCuzCourseCategory;
import org.edu.modules.maker.entity.VeCuzCourseMaker;
import org.edu.modules.summary.entity.VeCuzCourseSummary;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Api(tags={"课程表相关"})
@RestController
@RequestMapping({"/course/veCuzCourse"})
public class VeCuzCourseController
        extends BaseController<VeCuzCourse, IVeCuzCourseService>
{
    private static final Logger log = LoggerFactory.getLogger(VeCuzCourseController.class);
    @Autowired
    VeCuzCourseMapper veCuzCourseMapper;
    @Autowired
    private IVeCuzCourseService veCuzCourseService;
    @Value("${edu.path.urlPre}")
    private String fileUrlPre;

    @AutoLog("课程表相关-分页列表查询")
    @ApiOperation(value="课程表相关-分页列表查询", notes="课程表相关-分页列表查询")
    @GetMapping({"/list"})
    public Result<?> queryPageList(VeCuzCourse veCuzCourse, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req)
    {
        QueryWrapper<VeCuzCourse> queryWrapper = QueryGenerator.initQueryWrapper(veCuzCourse, req.getParameterMap());
        Page<VeCuzCourse> page = new Page(pageNo.intValue(), pageSize.intValue());
        IPage<VeCuzCourse> pageList = this.veCuzCourseService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @AutoLog("课程表相关-我创建的课程分页列表查询")
    @ApiOperation(value="课程表相关-我创建的课程", notes="课程表相关-我创建的课程")
    @GetMapping({"/MyCreatList"})
    public Result<?> MyCreatList(VeCuzCourse veCuzCourse, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req)
    {
        QueryWrapper<VeCuzCourse> queryWrapper = QueryGenerator.initQueryWrapper(veCuzCourse, req.getParameterMap());
        Page<Map<String, Object>> page = new Page(pageNo.intValue(), pageSize.intValue());
        int userId = 0;
        LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
//        userId = sysUser.getUserid().intValue();
        veCuzCourse.setCreateuserid(Integer.valueOf(userId));
        queryWrapper.setEntity(veCuzCourse);
        IPage<Map<String, Object>> pageList = this.veCuzCourseService.myCreatPageMaps(page, queryWrapper);
        for (Map item : pageList.getRecords()) {
            if (item.get("sourcefile") != null) {
                item.put("sourcefile", this.fileUrlPre + item.get("sourcefile"));
            }
        }
        return Result.OK(pageList);
    }

    @AutoLog("课程表相关-当前登录用户负责的课程分页列表查询")
    @ApiOperation(value="课程表相关-当前登录用户负责的课程", notes="课程表相关-当前登录用户负责的课程")
    @GetMapping({"/MyCourseList"})
    public Result<?> MyCourseList(VeCuzCourse veCuzCourse, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req)
    {
        QueryWrapper<VeCuzCourse> queryWrapper = QueryGenerator.initQueryWrapper(veCuzCourse, req.getParameterMap());
        Page<Map<String, Object>> page = new Page(pageNo.intValue(), pageSize.intValue());
        int userId = 0;
        LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
//        userId = sysUser.getUserid().intValue();

        queryWrapper.setEntity(veCuzCourse);
        IPage<Map<String, Object>> pageList = this.veCuzCourseService.myCreatPageMaps(page, queryWrapper);
        for (Map item : pageList.getRecords()) {
            if (item.get("sourcefile") != null) {
                item.replace("sourcefile", this.fileUrlPre + item.get("sourcefile"));
            }
        }
        return Result.OK(pageList);
    }

    @AutoLog("课程表相关-制作中的课程分页查询")
    @ApiOperation(value="制作中的课程分页查询", notes="课程表相关-制作中的课程分页查询")
    @PostMapping({"/unFinishList"})
    public Result<?> unFinishList(VeCuzCourse veCuzCourse, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req)
    {
        QueryWrapper<VeCuzCourse> queryWrapper = QueryGenerator.initQueryWrapper(veCuzCourse, req.getParameterMap());
        Page<Map<String, Object>> page = new Page(pageNo.intValue(), pageSize.intValue());
        queryWrapper.setEntity(veCuzCourse);
        IPage<Map<String, Object>> pageList = this.veCuzCourseService.unFinishPageMaps(page, queryWrapper);
        return Result.OK(pageList);
    }

    @AutoLog("课程表相关-制作完成的课程分页查询")
    @ApiOperation(value="制作完成的课程分页查询", notes="课程表相关-制作完成的课程分页查询")
    @PostMapping({"/finishedList"})
    public Result<List<VeCuzCourseVo>> finishedList(VeCuzCourseVo veCuzCourseVo, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, @RequestParam(value="sign", required=false) String sign, HttpServletRequest req)
    {
        Result<List<VeCuzCourseVo>> res = new Result();
        List<VeCuzCourseVo> ls = this.veCuzCourseService.finishedPageMaps(veCuzCourseVo, pageSize, pageNo);
        res.setResult(ls);
        res.setSuccess(true);
        return res;
    }

    @AutoLog("课程表相关-添加")
    @ApiOperation(value="课程表相关-添加", notes="课程表相关-添加")
    @PostMapping({"/add"})
    public Result<?> add(@RequestBody VeCuzCourseVo veCuzCourseVo)
    {
        VeCuzCourse veCuzCourse = new VeCuzCourse();
        BeanUtils.copyProperties(veCuzCourseVo, veCuzCourse);
        int date = TimeStampUtil.getNowSecondStamp();
        veCuzCourse.setCreatetime(Integer.valueOf(date));
        int createUserId = 0;
        int courseid = 0;


        LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
//        createUserId = sysUser.getUserid().intValue();
        String makerName = sysUser.getUsername();
        veCuzCourse.setCreateuserid(Integer.valueOf(createUserId));
        this.veCuzCourseMapper.insert(veCuzCourse);
        courseid = veCuzCourse.getId().intValue() + 1;

        int cateid = 0;
        cateid = veCuzCourseVo.getCateId().intValue();
        VeCuzCourseCategory veCuzCourseCategory = new VeCuzCourseCategory();

        veCuzCourseCategory.setCourseid(Integer.valueOf(courseid));
        veCuzCourseCategory.setCateid(veCuzCourseVo.getCateId());


        int makerid = 0;
//        makerid = sysUser.getUserid().intValue();
        VeCuzCourseMaker veCuzCourseMaker = new VeCuzCourseMaker();
        veCuzCourseMaker.setTerminalid(veCuzCourseVo.getTerminalid());
        veCuzCourseMaker.setCourseid(Integer.valueOf(courseid));
        veCuzCourseMaker.setMakerid(Integer.valueOf(createUserId));
        veCuzCourseMaker.setMakername(makerName);


        VeCuzCourseSummary veCuzCourseSummary = new VeCuzCourseSummary();
        veCuzCourseSummary.setCourseid(Integer.valueOf(courseid));
        veCuzCourseSummary.setContent(veCuzCourseVo.getContent());
        this.veCuzCourseService.save(veCuzCourse, veCuzCourseSummary, veCuzCourseCategory, veCuzCourseMaker);
        return Result.OK("添加成功！");
    }

    @AutoLog("课程表相关-编辑")
    @ApiOperation(value="课程表相关-编辑", notes="课程表相关-编辑")
    @PutMapping({"/edit"})
    public Result<?> edit(@RequestBody VeCuzCourse veCuzCourse)
    {
        this.veCuzCourseService.updateById(veCuzCourse);
        return Result.OK("编辑成功!");
    }

    @AutoLog("课程表相关-通过id删除")
    @ApiOperation(value="课程表相关-通过id删除", notes="课程表相关-通过id删除")
    @DeleteMapping({"/delete"})
    public Result<?> delete(@RequestParam(name="id", required=true) String id)
    {
        this.veCuzCourseService.removeById(id);
        return Result.OK("删除成功!");
    }

    @AutoLog("课程表相关-批量删除")
    @ApiOperation(value="课程表相关-批量删除", notes="课程表相关-批量删除")
    @DeleteMapping({"/deleteBatch"})
    public Result<?> deleteBatch(@RequestParam(name="ids", required=true) String ids)
    {
        this.veCuzCourseService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    @AutoLog("课程表相关-通过id查询")
    @ApiOperation(value="课程表相关-通过id查询", notes="课程表相关-通过id查询")
    @GetMapping({"/queryById"})
    public Result<?> queryById(@RequestParam(name="id", required=true) String id)
    {
        VeCuzCourse veCuzCourse = (VeCuzCourse)this.veCuzCourseService.getById(id);
        if (veCuzCourse == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(veCuzCourse);
    }

    @RequestMapping({"/exportXls"})
    public ModelAndView exportXls(HttpServletRequest request, VeCuzCourse veCuzCourse)
    {
        return super.exportXls(request, veCuzCourse, VeCuzCourse.class, "课程表相关");
    }

    @ApiOperation(value="导入用户管理信息", notes="导入用户管理信息")
    @RequestMapping(value={"/importExcel"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response)
    {
        return super.importExcel(request, response, VeCuzCourse.class);
    }

    @ApiOperation(value="做课-上传文件图片", notes="课程表相关-添加")
    @PostMapping(value={"/upload"}, consumes={"multipart/form-data"})
    public Result<?> add(@RequestParam("file") MultipartFile file)
            throws Exception
    {
        String s = OSSUtils.ossUrl(file);
        System.out.println(s);

        return Result.OK(s);
    }

    @AutoLog("推荐课程-分页查询")
    @ApiOperation(value="推荐课程分页查询", notes="推荐课程-分页查询")
    @PostMapping({"/pageList"})
    public Result<?> pageList(VeCuzCourse veCuzCourse, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req)
    {
        QueryWrapper<VeCuzCourse> queryWrapper = QueryGenerator.initQueryWrapper(veCuzCourse, req.getParameterMap());
        Page<Map<String, Object>> page = new Page(pageNo.intValue(), pageSize.intValue());
        queryWrapper.setEntity(veCuzCourse);
        IPage<Map<String, Object>> pageList = this.veCuzCourseService.pageMaps(page, queryWrapper);
        return Result.OK(pageList);
    }

    @AutoLog("课程-分页查询")
    @ApiOperation(value="课程分页模糊查询", notes="课程-分页查询")
    @PostMapping({"/coursePageList"})
    public Result<?> coursePageList(VeCuzCourse veCuzCourse, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req)
    {
        QueryWrapper<VeCuzCourse> queryWrapper = QueryGenerator.initQueryWrapper(veCuzCourse, req.getParameterMap());
        Page<Map<String, Object>> page = new Page(pageNo.intValue(), pageSize.intValue());
        queryWrapper.setEntity(veCuzCourse);
        IPage<Map<String, Object>> pageList = this.veCuzCourseService.coursePageMaps(page, queryWrapper);
        return Result.OK(pageList);
    }
}
